﻿package com.sykj.manage.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.manage.service.SysParamService;
import com.sykj.manage.model.SysParam;
import com.sykj.util.ManageBody;
import com.sykj.util.PageUtil;
import com.sykj.manage.service.SysParamService;
import com.sykj.manage.model.BisShopUser;
import com.sykj.manage.model.SysParam;

/**
 * 根据键查值
 * @author 蔡聪
 *	2018年9月30日上午9:44:04
 */
@Controller
@RequestMapping("/sysparam")
public class SysParamController {
	@Autowired
	SysParamService sysparamservice;

	/**
	 * 
	 * @param sysParam
	 * @return
	 * 键值参数
	 */
	@RequestMapping("/findParam")
	@ResponseBody
	public ManageBody finduser(SysParam sysParam,Integer offset,Integer pageSize){
		PageHelper.startPage(offset==null?1:PageUtil.getPageNum(offset, pageSize),pageSize==null?10:pageSize,true);
		List<SysParam> params = sysparamservice.findParams(sysParam);
		PageInfo<SysParam> pageInfo = new PageInfo<SysParam>(params);
		return ManageBody.getData((int)pageInfo.getTotal(), pageInfo.getList(), "");
		
	}
	
	/**
	 * 删除键值参数
	 * @param param_id
	 * @return
	 */
	@RequestMapping("/delParams")
	@ResponseBody
	public Object delParams(@RequestParam("param_id")String[] param_id) {
		int cnt = sysparamservice.delParams(param_id);
		return cnt;
	}
	
	/**
	 * 根据param_id查询参数
	 * @param param_id
	 * @return
	 */
	@RequestMapping("/findParamById")
	@ResponseBody
	public Object findParamById(@RequestParam("param_id")String param_id) {
		SysParam param = sysparamservice.findParamById(param_id);
		return param;
	}
	
	/**
	 * 添加参数
	 * @param sysParam
	 * @return
	 */
	@RequestMapping("/addParam")
	@ResponseBody
	public Object addParam(SysParam sysParam){
		Date date = new Date();
		sysParam.setCreate_time(date);
		int addcnt = sysparamservice.addParam(sysParam);
		return addcnt;
	}
	
	/**
	 * 修改参数
	 * @param param_ids
	 * @return
	 */
	@RequestMapping("/updParam")
	@ResponseBody
	public Object updParam(SysParam sysParam) {
		Date date = new Date();
		sysParam.setModify_time(date);
		int updcnt = sysparamservice.updParam(sysParam);
		return updcnt;
	}
	
		//循环查出数据装进sessi里面
		@RequestMapping("/allsysparamKey")
		@ResponseBody
		public Map<String, SysParam> allsysparamKey(HttpSession session){
			Map<String, SysParam> paramMaps = sysparamservice.allSysparamdao();
			session.setAttribute("sysparam", paramMaps);
			System.out.println("~~~"+paramMaps);
			return paramMaps;
		}
		
		//修改先从sessi里拿出来然后在装进list拿出来修改
		@RequestMapping("/update")
		@ResponseBody
		public Map<String, SysParam> update(String subscribe_deposit, String every_two_limit, String cancel_time ,HttpSession session){
			Map<String, SysParam> paramMaps = (Map<String, SysParam>) session.getAttribute("sysparam");
			SysParam sysParam1 = paramMaps.get("subscribe_deposit");
			SysParam sysParam2 = paramMaps.get("every_two_limit");
			SysParam sysParam3 = paramMaps.get("cancel_time");
		
			
			List<SysParam> paramsList = new ArrayList<SysParam>();
			sysParam1.setParam_value(subscribe_deposit);
			paramsList.add(sysParam1);
			sysParam2.setParam_value(every_two_limit);
			paramsList.add(sysParam2);
			sysParam3.setParam_value(cancel_time);
			paramsList.add(sysParam3);
			
			sysparamservice.updateSysParams(paramsList);
			return paramMaps;
		}
		//修改先从sessi里拿出来然后在装进list拿出来修改
				@RequestMapping("/update2")
				@ResponseBody
				public Map<String, SysParam> update(String sms_url,String sms_app_key,String sms_app_secret,String sms_signe,String sms_template_code, HttpSession session){
					Map<String, SysParam> paramMaps = (Map<String, SysParam>) session.getAttribute("sysparam");
					
					SysParam sysParam4 = paramMaps.get("sms_url");
					SysParam sysParam5 = paramMaps.get("sms_app_key");
					SysParam sysParam6 = paramMaps.get("sms_app_secret");
					SysParam sysParam7 = paramMaps.get("sms_signe");
					SysParam sysParam8 = paramMaps.get("sms_template_code");
					
					List<SysParam> paramsList = new ArrayList<SysParam>();
					
					sysParam4.setParam_value(sms_url);
					paramsList.add(sysParam4);
					sysParam5.setParam_value(sms_app_key);
					paramsList.add(sysParam5);
					sysParam6.setParam_value(sms_app_secret);
					paramsList.add(sysParam6);
					sysParam7.setParam_value(sms_signe);
					paramsList.add(sysParam7);
					sysParam8.setParam_value(sms_template_code);
					paramsList.add(sysParam8);
					
					sysparamservice.updateSysParams(paramsList);
					return paramMaps;
				}
		
}
